package com.capgemini.cn.authority.rest.service.impl;

import com.capgemini.cn.authority.data.entity.Asserts;
import com.capgemini.cn.authority.data.entity.Authorities;
import com.capgemini.cn.authority.data.repository.AssertsRepository;
import com.capgemini.cn.authority.data.repository.AuthoritiesRepository;
import com.capgemini.cn.authority.rest.exception.AuthRestBusinessException;
import com.capgemini.cn.authority.rest.service.AbstractBaseService;
import com.capgemini.cn.authority.rest.service.AssetService;
import com.capgemini.cn.authority.rest.valueobject.DataResponse;
import com.capgemini.cn.authority.rest.valueobject.data.AssetVO;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

/**
 * Created at 2017/11/1
 *
 * @author Andriy
 */
@Service(value = AssetServiceImpl.COMPONENT_NAME)
public class AssetServiceImpl extends AbstractBaseService<AssetVO, Asserts> implements AssetService {

    public static final String COMPONENT_NAME = "assetService";

    private final String[] IGNORE_PROPERTIES = {
    };

    @Autowired
    @Setter
    private AssertsRepository assertsRepository;

    @Autowired
    @Setter
    private AuthoritiesRepository authoritiesRepository;

    @Transactional
    @Override
    public DataResponse<List<AssetVO>> findAll(int page, int size, String orderProp, Sort.Direction direction) {
        return super.findAll(AssetVO.class, page, size, orderProp, direction, this.IGNORE_PROPERTIES);
    }

    @Transactional
    @Override
    public DataResponse<AssetVO> findById(long id) {
        return null;
    }

    @Transactional
    @Override
    public DataResponse<AssetVO> add(AssetVO source) {
        return null;
    }

    @Transactional
    @Override
    public boolean add(List<AssetVO> sourceList) {
        return false;
    }

    @Transactional
    @Override
    public DataResponse<AssetVO> update(long id, Map<String, Object> properties) throws AuthRestBusinessException {
        return null;
    }

    @Transactional
    @Override
    public boolean update(Map<String, Object> properties, List<AssetVO> sourceList) {
        return false;
    }

    @Transactional
    @Override
    public DataResponse<List<AssetVO>> findAssetsByAuthorize(long authId) {
        Authorities authority = this.authoritiesRepository.findOne(authId);
        if (ObjectUtils.isEmpty(authority)) {
            throw new AuthRestBusinessException("无法根据主键ID（" + authId + "）查询到对应的权限实体对象信息！");
        }
        DataResponse<List<AssetVO>> result = new DataResponse<>();
        List<Asserts> entities = this.assertsRepository.findAuthorityOwnedAsset(authority.getId());
        result.setResponse(super.conversionToValueObject(entities, AssetVO.class, this.IGNORE_PROPERTIES));
        return result;
    }
}
